###
# makefile
##
## 08-MARCH-2012


APPNAME		= gwwdl
OFLAG		= -O2

## please pass these into the app / sync with build counting
APP_VERSION_MAJOR=	2012
APP_VERSION_MINOR=	03
APP_VERSION_RELEASE=	8
APP_VERSION_REVISION=	1
APP_VERSION=$(APP_VERSION_MAJOR).$(APP_VERSION_MINOR).$(APP_VERSION_RELEASE).$(APP_VERSION_REVISION)

### PLATFORM 
PLATFORM	= MSWIN
#PLATFORM= GTK
#PLATFORM= X11
#PLATFORM= CURSES
#PLATFORM= NONE
PDDEF		= -DGW_PLATFORM_$(PLATFORM)


### PLIBS
# windows set, with mm
PLIBS		= -lcomctl32 -lgdi32 -lwinmm

PLDFLAGS	= -Wl,-subsystem,windows

### PPREFIX
## for cross-compilation, should be detected / automated
PPREFIX		= i686-w64-mingw32
PSEPARATOR	= -
### CSA
## compiler-specific appname
# defaults, GNU
CSA_CC 		= gcc
CSA_CXX 	= g++
# MS-WIN COMPATIBILITY
CSA_RC		= windres


CC			= $(PPREFIX)$(PSEPARATOR)$(CSA_CC)
CXX			= $(PPREFIX)$(PSEPARATOR)$(CSA_CXX)
RC			= $(PPREFIX)$(PSEPARATOR)$(CSA_RC)

#util
MKDIR		= mkdir
TAR			= tar

CFLAGS		= $(OFLAG) -Wall $(PDDEF)
CXXFLAGS	= $(CFLAGS)

BINNAME		= $(APPNAME)
BINEXT		= .exe
BINFULL		= $(BINNAME)$(BINEXT)
#fixed:
BINFULLV	= $(BINNAME)-$(APP_VERSION)$(BINEXT)

BINTARGET	= $(PATHBIN)$(BINFULLV)
TARTARGET	= $(PATHTAR)$(BINFULLV).tar.gz

# main sources to be compiled:
PATHSRC		= src/
# resources in subpath:
PATHRES		= $(PATHSRC)res/

# these will be created if necessary during build
PATHBUILD	= build/
PATHOBJ		= build/obj/
PATHBIN		= build/bin/
PATHTAR		= build/tar/
PATHLOG     = build/log/

BUILDCOUNTER= $(PATHLOG)build.counter
BUILDLOG    = $(PATHLOG)build.log
TEELOG      = $(PATHLOG)tee.log

CXXCORESRC	:= $(wildcard $(PATHSRC)*.cpp)
#CCCROESRC	= $(wildcard $(PATHSRC)*.c)
COREOBJ		:= $(patsubst $(PATHSRC)%.cpp,$(PATHOBJ)%.o,$(CXXCORESRC))
#FIX:: windows resources
RESSRC		:= $(wildcard $(PATHRES)*.rc)
RESOBJ		:= $(patsubst $(PATHRES)%.rc,$(PATHOBJ)%.res,$(RESSRC))

.PHONY: all

all: pre_build init_directories $(BINTARGET) post_build

clean: pre_clean init_directories my_clean post_clean

#distclean

pre_build:
	@echo "BUILD $(BINTARGET) >>";

pre_clean:
	@echo "CLEAN >>";

init_directories:
	@echo -n "  += verify $(PATHLOG) ...";
	@mkdir -p $(PATHLOG)
	@echo " ok";
	@echo -n "  += verify $(PATHOBJ) ...";
	@mkdir -p $(PATHOBJ)
	@echo " ok";
	@echo -n "  += verify $(PATHBIN) ...";
	@mkdir -p $(PATHBIN)
	@echo " ok";

my_clean:
	@echo -n "  - $(PATHOBJ)* ...";
	@rm -fr $(PATHOBJ)*
	@echo " ok";
	@echo -n "  - $(PATHBIN)*";
	@rm -f $(PATHBIN)*
	@echo " ok";


post_clean:
	@echo "<<";

post_build:
	@echo -n "  =  incrementing build counter... ";
	@echo -n "X" >> $(BUILDCOUNTER);
	@echo -n " ok\n     Build #: ";
	@stat -c%s $(BUILDCOUNTER);
	@echo -n "  =  updating build log... ";
	@date >> $(BUILDLOG)
	@echo -n "  =  creating symbolic link to $(BINFULL)... ";
	@ln -sf -T $(BINTARGET) $(BINFULL)
	@echo " ok";
	@echo "<<";

exec:
	@echo "\n./$(BINTARGET)...";
	@clear
	@$(BINTARGET)

tar:
	@echo "TAR $(TARTARGET) >>";
	@echo -n "  += verify $(PATHTAR) ...";
	@$(MKDIR) -p $(PATHTAR)
	@echo " ok";
	@echo -n "  + $(TARTARGET) ...";
	@$(TAR) -zcf $(TARTARGET) *
	@echo " ok";
	@echo "<<";

$(BINTARGET): ${COREOBJ} ${RESOBJ}
	@echo -n "  + " $^
	$(CXX) $(CXXFLAGS) $(PLDFLAGS) -o $(BINTARGET) $^ $(PLIBS)
	@echo " success!";

$(PATHOBJ)%.o: $(PATHSRC)%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

$(PATHOBJ)%.res: $(PATHRES)%.rc
	$(RC) -O Coff $< $@


